// ShopFragment.java
package com.example.jinongheadlines.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jinongheadlines.R;
import com.example.jinongheadlines.adapters.ProductAdapter;
import com.example.jinongheadlines.models.Product;

import java.util.ArrayList;
import java.util.List;

public class ShopFragment extends Fragment {

    private RecyclerView rvProducts;
    private ProductAdapter productAdapter;
    private List<Product> productList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop, container, false);
        rvProducts = view.findViewById(R.id.rvProducts);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化商品数据
        initProducts();

        // 设置RecyclerView
        productAdapter = new ProductAdapter(getActivity(), productList);
        rvProducts.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvProducts.setAdapter(productAdapter);
    }

    private void initProducts() {
        // 这里使用模拟数据，实际开发中应从网络或数据库获取
        productList.add(new Product("1", "有机大米", "优质东北有机大米，5kg装", 59.9, "https://example.com/image1.jpg"));
        productList.add(new Product("2", "新鲜苹果", "烟台红富士苹果，10斤装", 39.9, "https://example.com/image2.jpg"));
        productList.add(new Product("3", "土鸡蛋", "农家散养土鸡蛋，30枚装", 45.0, "https://example.com/image3.jpg"));
        productList.add(new Product("4", "有机蔬菜礼盒", "时令有机蔬菜组合", 88.0, "https://example.com/image4.jpg"));
        productList.add(new Product("5", "农家蜂蜜", "纯天然百花蜜，500g", 68.0, "https://example.com/image5.jpg"));
    }
}